package com.ws.shop.action.admin;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.bean.Page;
import com.ws.shop.entity.Order;
import com.ws.shop.service.OrderService;
import com.ws.shop.util.DateUtil;
import com.ws.shop.util.RequestUtil;

@Controller
@RequestMapping(value="/admin/order/*")
public class OrderAction extends BaseAdminAction{
	
	private static Logger log = Logger.getLogger(OrderAction.class);
	@Autowired
	private OrderService orderService;
	
	@RequestMapping(value="index.jhtml",method=RequestMethod.GET)
	public String index(HttpServletRequest request,Model model){
		String start = RequestUtil.getStringParameter(request, "start", null);
		String end = RequestUtil.getStringParameter(request, "end", null);
		String q_name = RequestUtil.getStringParameter(request, "q_name", null);
		int aid = RequestUtil.getIntParameter(request, "aid", 0);
		int type = RequestUtil.getIntParameter(request, "type", -1);
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		if(start == null || end == null){
			start = DateUtil.dateToString(DateUtil.newDate(new Date(), -6), DateUtil.YYYY_MM_DD);
			end = DateUtil.dateToString(new Date(), DateUtil.YYYY_MM_DD);
		}
		Page page = orderService.findOrderListPage(pageno,0,q_name,start,end,aid,type);
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		model.addAttribute("q_name", q_name);
		model.addAttribute("aid", aid);
		model.addAttribute("type", type);
		model.addAttribute(Page.PAGE, page);
		model.addAttribute("typeDescs", Order.TYPE_DESCS);
		model.addAttribute("stateDescs", Order.STATE_DESCS);
		return "order_index";
	}

	@RequestMapping(value="view.jhtml",method=RequestMethod.GET)
	public String view(HttpServletRequest request,Model model){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		Order order = orderService.findOrderById(id);
		model.addAttribute("order", order);
		model.addAttribute("typeDescs", Order.TYPE_DESCS);
		model.addAttribute("stateDescs", Order.STATE_DESCS);
		return "order_view";
	}
	
	public String down(HttpServletRequest request){
		String start = RequestUtil.getStringParameter(request, "start", null);
		String end = RequestUtil.getStringParameter(request, "end", null);
		String name = RequestUtil.getStringParameter(request, "name", null);
		int aid = RequestUtil.getIntParameter(request, "aid", 0);
		int type = RequestUtil.getIntParameter(request, "type", 0);
		
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		Page page = orderService.findOrderListPage(pageno,0,name,start,end,aid,type);
		//TODO 导出Excel 格式： ID 订单号 竞拍ID 用户名 商品名称 订单价格 地址 邮编 联系方式 订单类型 添加时间 订单状态 备注
		
		return null;
	}
	
	/**
	 * 设置为付款状态
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="setpaid.jhtml",method=RequestMethod.POST)
	public String setpaid(HttpServletRequest request,HttpServletResponse response){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		if(id <= 0){
			return this.ajaxJsonErrorMessage(response, "请选择订单");
		}
		try{
			orderService.updateOrderState(id,1);
			return this.ajaxJsonSuccessMessage(response, "操作成功");
		}catch(Exception e){
			log.error("设置订单已付款状态失败",e);
			return this.ajaxJsonErrorMessage(response, "系统异常，请稍后再试");
		}
	}
	
	/**
	 * 设置为已成交状态
	 * @return
	 */
	@RequestMapping(value="setexecuted.jhtml",method=RequestMethod.POST)
	public String setexecuted(HttpServletRequest request,HttpServletResponse response){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		if(id <= 0){
			return this.ajaxJsonErrorMessage(response, "请选择订单");
		}
		try{
			orderService.updateOrderState(id,3);
			return this.ajaxJsonSuccessMessage(response, "操作成功");
		}catch(Exception e){
			log.error("设置订单已成交状态失败",e);
			return this.ajaxJsonErrorMessage(response, "系统异常，请稍后再试");
		}
	}
	
}
